package com.link.base.base.attendance.service;

import com.link.base.base.attendance.dao.mybatis.mapper.AttendanceMapper;
import com.link.base.base.attendance.model.Attendance;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author  unknwon
 * @date:   2018/11/8 15:51
 * @version 1.0
 */

@Service
public class AttendanceServiceImpl extends BasicServiceImpl<Attendance> implements AttendanceService {
    @Resource
    private AttendanceMapper attendanceMapper;

    @Override
    public BasicMapper<Attendance> getBasicMapper() throws Exception {
        return attendanceMapper;
    }

    /**
     * 按月查询
     * @param entity 实体类
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Attendance> queryAllByMonth(Attendance entity) throws Exception {


        Map<String, Attendance> result = new HashMap<>(16);
        // 传入参数日期
        // 根据传入的月份,获取当月的所有日期
        Date inputDate = DateUtil.stringToDate(entity.getCurrentMonth(), "yyyy-MM-dd");

        Date monthStart = DateUtil.getMonthStart(inputDate);
        Date monthEnd = DateUtil.getMonthEnd(inputDate);
        // 查询整月的全部任务
        entity.setMonthStart(DateUtil.dateToStr(monthStart, "yyyy-MM-dd"));
        entity.setMonthEnd(DateUtil.dateToStr(monthEnd, "yyyy-MM-dd"));
        List<Attendance> attendances = attendanceMapper.queryAllByMonth(entity);

        if (attendances != null && attendances.size() > 0) {
            List<String> daysList = DateUtil.getAllDaysOfMonth(inputDate);
            for (String date : daysList) {
                for (Attendance attendance : attendances) {
                    if (date.equals(attendance.getCreated())) {
                        result.put(date, attendance);
                        break;
                    }
                }
            }
        }
        return result;
    }
}
